<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Import\TransverseImport;
use App\Admin\Actions\Tools\TransverseTagBatchAction;
use App\Services\TransverseService;
use App\Transverse;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class TransverseController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '横向课题';
    protected $service;

    public function __construct(TransverseService $service)
    {
        $this->service = $service;
    }

    public function index(Content $content)
    {
        $grid = $this->grid();
        return $content
            ->title($this->title())
            ->description(trans('admin.list'))
            ->body($grid);
    }

    protected function header(Grid $grid)
    {
        $this->service->header($grid);
    }

    protected function tools(Grid $grid)
    {
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new  TransverseTagBatchAction());
            $tools->append(new TransverseImport());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Transverse());
        $this->header($grid);
        $this->tools($grid);
        $this->service->gridColumn($grid, 1, 1);
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Transverse());
        $this->service->form($form, 1);
        return $form;
    }

    protected function editableForm()
    {
        $form = new Form(new Transverse());
        $form->hidden('contract_no');
        return $form;
    }


    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update($id)
    {
        if (request()->ajax() && request()->get('_editable')) {
            return $this->editableForm()->update($id);
        }
        return $this->form()->update($id);
    }


}
